<?php 
/*
Plugin Name: post-image
Plugin URI: http://akismet.com/?return=true
Description: Use to add image to article.
Version: 2.5.6
Author: ccy.news
Author URI: http://automattic.com/wordpress-plugins/
License: GPLv2 or later
*/

add_action('submitpage_box', 'post_image_load_languages');
add_action('submitpage_box', 'add_post_image_box');
add_action('submitpost_box', 'post_image_load_languages');
add_action('submitpost_box', 'add_post_image_box');

add_action('admin_print_styles', 'post_image_load_css');
add_action('admin_enqueue_scripts', 'post_image_enqueue_scripts');
add_action('save_post', 'post_image_save_post');

function post_image_load_languages(){
	load_plugin_textdomain('post_image', false, dirname(plugin_basename( __FILE__ )).'/languages');
}
function add_post_image_box(){
	add_meta_box('post_image_id', __('post image', 'post_image'), 'post_image_func', null, 'normal', 'core');
}

function post_image_func($post, $box){
	$big_src = get_post_meta($post->ID, '_post_image_big', true);
	$small_src = get_post_meta($post->ID, '_post_image_small', true);
	?>
	<div id="post_image_box">
	<div id="post_image_box_big">
	<img id="post_image_big_img" src="<?php echo $big_src;?>" alt="<?php __('Big image', 'post_image')?>"/><br/>
	<input type="text" id="post_image_big_input_id" name="post_image_big_input_name" value="<?php echo $big_src;?>">
	<input type="button" id="post_image_big_preview" name="post_image_big_preview_name" value="<?php _e('Big image preview', 'post_image');?>" class="preview button">
	</div>
	<div id="post_image_box_small">
	<img id="post_image_small_img" src="<?php echo $small_src;?>" alt="<?php __('Small image', 'post_image')?>"/><br/>
	<input type="text" id="post_image_small_input_id" name="post_image_small_input_name" value="<?php echo $small_src;?>">
	<input type="button" id="post_image_small_preview" name="post_image_small_preview_name" value="<?php _e('Small image preview', 'post_image');?>" class="preview button">
	</div>
	</div>
<?php wp_nonce_field(plugin_basename(__FILE__),'post_image_noncename');
}

function post_image_load_css(){
	wp_enqueue_style('post_image_styles', plugins_url('/css/main.css', __FILE__));
}

function post_image_enqueue_scripts(){
	wp_enqueue_script('post_image_script', plugins_url('/js/main.js', __FILE__), array('jquery'));
}

function post_image_save_post($post_id){
	if (!wp_verify_nonce($_POST['post_image_noncename'], plugin_basename(__FILE__))) {
		return;
	}
	
	if(isset($_POST['post_image_big_input_name'])){
		update_post_meta($post_id, '_post_image_big', sanitize_text_field($_POST['post_image_big_input_name']));
	}
	if(isset($_POST['post_image_small_input_name'])){
		update_post_meta($post_id, '_post_image_small', sanitize_text_field($_POST['post_image_small_input_name']));
	}
}
?>

